package com.sf.fy.uav.applet.order.controller;

import com.sf.fy.uav.common.enums.DecryptTypeEnum;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.response.user.UserDetailResponse;
import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.order.entity.Feedback;
import com.sf.fy.uav.order.mapper.FeedbackMapper;
import com.sf.fy.uav.order.service.IFeedbackService;
import com.sf.fy.uav.user.entity.User;
import com.sf.fy.uav.user.mapper.UserMapper;
import com.sf.fy.uav.user.rest.UserRest;
import com.sf.fy.uav.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author LTQ
 */
@RestController
@RequestMapping("/home/user/")
public class SysUserController {

    @Autowired
    private UserRest userRest;

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PhoneEncoderUtil phoneEncoderUtil;


    @GetMapping("get-users")
    @CrossOrigin
    public Rs<List<UserDetailResponse>> getUsers(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String phoneNumber, @RequestParam(required = false) Integer platformCode) {
        return Rs.success(userRest.getUsers(page, size, phoneNumber, platformCode));
    }

    @GetMapping("get-total")
    @CrossOrigin
    public Rs<Long> getTotal(@RequestParam(required = false) String phoneNumber, @RequestParam(required = false) Integer platformCode) {
        return Rs.success(userRest.getTotal(phoneNumber, platformCode));
    }

    @PostMapping("freeze")
    @CrossOrigin
    public Rs<Void> freeze(@RequestBody IdRequest idRequest) {
        userRest.freeze(idRequest);
        return Rs.success();
    }

    @PostMapping("restore")
    @CrossOrigin
    public Rs<Void> restore(@RequestBody IdRequest idRequest) {
        userRest.restore(idRequest);
        return Rs.success();
    }


    @GetMapping("decrypt")
    @CrossOrigin
    public Rs<String> decrypt(@RequestParam() String id, @RequestParam() Integer type) {
        String phoneNumber = null;
        if (DecryptTypeEnum.FEEDBACK.getCode().equals(type)) {
            Feedback feedback = feedbackMapper.selectById(id);
            phoneNumber = feedback.getPhoneNumber();
        } else if (DecryptTypeEnum.USER.getCode().equals(type)) {
            User user = userMapper.selectById(id);
            phoneNumber = user.getPhoneNumber();
        }
        return Rs.success(phoneEncoderUtil.decryptString(phoneNumber));
    }


}
